package horncomb.bpm.task.instance;

import horncomb.bpm.process.core.Token;
import horncomb.bpm.task.core.TaskDefinition;
import horncomb.bpm.task.core.TaskInstance;
import horncomb.bpm.task.core.TaskInstanceCreator;
import horncomb.bpm.task.def.DefaultTaskDefinition;

import java.util.Date;
import java.util.UUID;

/**
 * @author Daniel
 */
public class DefaultTaskInstanceCreator implements TaskInstanceCreator {
	/**
	 * {@inheritDoc}
	 */
	public TaskInstance create(Token token, TaskDefinition taskDefinition,
			Object contextModel) {
		DefaultTaskInstance task = new DefaultTaskInstance();
		task.setId(taskDefinition.getId() + ":" + UUID.randomUUID());
		// TODO task name
		task.setName(taskDefinition.getName());
		task.setToken(token);
		task.setCreatedDate(new Date());
		if (taskDefinition instanceof DefaultTaskDefinition) {
			task.setHiding(((DefaultTaskDefinition) taskDefinition)
					.isDefaultHiding());
		}
		if (taskDefinition.getTaskAssignment() != null) {
			taskDefinition.getTaskAssignment().assign(task);
		}
		return task;
	}
}
